<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_Sanpham extends ZF_DB{
    protected $_table_name = "sanpham" ;
    
    protected $_masanpham;
    protected $_tensanpham;
    protected $_dongia;
    protected $_manhacungcap;
    protected $_maloaisanpham;
    
    public function getMaSanpham(){
        return $this->_masanpham;
    }
    public function setMaSanpham($masanpham){
        $this->_masanpham = $masanpham;
    }
    public function getTenSanpham(){
        return $this->_tensanpham;
    }
    public function setDonGia($dongia){
        $this->_dongia = $dongia;
    }
    public function getDonGia(){
        return $this->_dongia;
        
    }
    public function setMaNhaCungCap($manhacungcap){
       $this->_manhacungcap = $manhacungcap;
        
    }
    public function getMaNhaCungCap(){
        return $this->_manhacungcap;
        
    }
    public function setMaLoaiSanPham($maloaisanpham){
        $this->_maloaisanpham = $maloaisanpham;
    }
    public function getMaLoaiSanPham(){
        return $this->_maloaisanpham;
    }
    
    public function insert($name,$name_en,
            $description = "",$description_en = "",
            $phone ="",$city = 0,$address ="",
            $images ="",
            $rate = 0,$counter = 0,$active=1){
        
        if($name){
                $data =  array(
                    'name' =>$name,
                    'name_en'=>$name_en,
                    'description' =>$description,
                    'description_en'=>$description_en,
                    'images'=>$images,
                    'phone'=>$phone,
                    'address'=>$address,
                    'city' =>$city,
                    'active'=>$active);
            return         $this->db->insert($this->_table_name,$data);
	
	}
        return -1;
	
    }
   
    public function update(){
        $where  = "id = $this->_id";
	$data =  array(
                    'name' =>$this->_name,
                    'name_en'=>$this->_name_en,
                    'address' =>$this->_address,
                    'phone'=>$this->_phone,
                    'description'=>$this->_description,
                    'description_en'=>$this->_description_en,
                    'city'=>$this->_city,
                    'images'=>$this->_images,
                    'active'=>$this->_active);
        
	return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    public function getById($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.MaSanPham = ?",$id );
		
		$data = $this->db->fetchRow($select);
		if($data){
			$this->_masanpham = $data['MaSanPham'];
			$this->_tensanpham = $data['TenSanPham'];
			$this->_dongia = $data['DonGia'];
			$this->_manhacungcap = $data['MaNhaCungCap'];
                        $this->_maloaisanpham = $data['MaLoaiSanPham'];
		}
                return $data;
	}
	return -1;
    }
   
    public function getByMaNhaCungCap($manhacungcap){
        if($manhacungcap)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.MaNhaCungCap = ?",$manhacungcap);
		
		$data = $this->db->fetchAll($select);
		return $data;
	}
	return -1;
    }
   
    
    public function listAll(){
        $select = $this->db->select()
                ->from($this->_table_name);
        $data = $this->db->fetchAll($select);
	return $data;
    }
    public function delete(){
            $this->getById($this->_id);
            $this->_active = 0;
            return $this->update();
    }
}
?>
